<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>热量计算</title>
	<link rel="stylesheet" type="text/css" href="http://bobo.bj.bcebos.com/bootstrap.min.css">
	<script src="http://bobo.bj.bcebos.com/jquery.min.js"></script>
	<style type="text/css">
		ul{
			list-style: none;
		}
		li{
			width: 200px;
			height: 32px;
			border: 1px solid #ccc;
			float: left;
			margin: 10px;
		}
		input{
			width: 50px;
			display: inline-block;
		}
		.box{
			width: 500px;
			margin: 0 auto;
		}
	</style>
</head>
<body>
	<ul>
		<li><input type="text" data-nl="174">碗米饭</li>
		<li><input type="text" data-nl="752">盘炒饭</li>
		<li><input type="text" data-nl="48">碗豆浆</li>
		<li><input type="text" data-nl="280">个肉包</li>
		<li><input type="text" data-nl="71">个鸡蛋</li>
		<li><input type="text" data-nl="268">个馒头</li>
		<li><input type="text" data-nl="459">个红薯</li>
		<li><input type="text" data-nl="156">个苹果</li>
		<li><input type="text" data-nl="135">盒牛奶</li>
		<li><input type="text" data-nl="215">瓶可乐</li>
		<li><input type="text" data-nl="36">块西瓜[24块一个]</li>
		<li><input type="text" data-nl="1">大卡[其他菜]</li>
	</ul>
	<button id="js" type="button" class="btn btn-primary">计算</button>
	<div class="form-horizontal box">
	  <div class="form-group">
	    <label for="nlval" class="col-sm-2 control-label">能量：</label>
	    <div class="col-sm-10">
	      <input type="text" class="form-control" id="nlval">
	    </div>
	  </div>
	  <div class="form-group">
	    <label for="tzval" class="col-sm-2 control-label">体重：</label>
	    <div class="col-sm-10">
	      <input type="text" class="form-control" id="tzval">
	    </div>
	  </div>
	  <div class="form-group">
	    <label for="ydval" class="col-sm-2 control-label">运动：</label>
	    <div class="col-sm-10">
	      <input type="text" class="form-control" id="ydval">
	    </div>
	  </div>
	  <div class="form-group">
	    <div class="col-sm-offset-2 col-sm-10">
	      <button id="submitfs" type="button" class="btn btn-primary col-lg-10">录入</button>
	    </div>
	  </div>
	</div>
	<script>
		var date   = new Date();
		var endm   = date.getMonth()+1>9?date.getMonth()+1:"0"+(date.getMonth()+1);
		var endday = date.getDate()>9?date.getDate():"0"+date.getDate();
		var time   = date.getFullYear()+"-"+endm+"-"+endday;
		$("#js").click(function(){
			var kll = 0;
			$("input").each(function(){
				if($(this).val()){
					var number = parseInt($(this).val());
					var dw = parseInt($(this).attr("data-nl"));
					kll += number*dw;
				}
			});
			$("#nlval").val(kll);
		});
		$("#submitfs").click(function(){
			var saveobj = {
				"time":time,
				"weight":$("#tzval").val(),
				"movement":$("#ydval").val(),
				"energy":$("#nlval").val()
			};
			saveDay(saveobj);
		});
		function saveDay(obj){
			$.ajax({
				url: 'tzserver.php?act=add',
				type: 'POST',
				dataType: 'html',
				data: {mydata: JSON.stringify(obj)},
			})
			.done(function(data) {
				if(data == "success"){
					alert(data);
				}else{
					alert("添加失败");
				}
			})
			.fail(function() {
				console.log("error");
			})
			.always(function() {
				console.log("--------------");
			});
		}
	</script>
</body>
</html>